#include <Windows.h>
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) // 设置入口地址(隐藏控制台)

int main() {

	//找到扫雷窗口句柄
	HWND hwnd = FindWindow(NULL, L"扫雷");
	if (hwnd == NULL) {
		MessageBox(NULL, L"游戏未打开", L"Tip", MB_OK);
		return 0;
	}

	//根据句柄得到进程id
	DWORD saolei_id;
	GetWindowThreadProcessId(hwnd, &saolei_id);
	
	//根据进程id得到进程句柄
	HANDLE h_process = OpenProcess(PROCESS_VM_READ, FALSE, saolei_id);
	if (h_process == NULL) {
		//std::cout << "内存读取失败" << std::endl;
		return 0;
	}

	int hang = 0;
	int lie = 0;
	unsigned int 雷区基址 = 0x01005361;
	unsigned int 雷区宽度 = 0x01005334;
	unsigned int 雷区高度 = 0x01005338;

	unsigned int buffer;
	SIZE_T dwNumberOfBytesRead;
	if (!ReadProcessMemory(h_process, (LPCVOID)雷区宽度, &buffer, 4, &dwNumberOfBytesRead)) {
		return 0;
	}
	hang = buffer;
	
	if (!ReadProcessMemory(h_process, (LPCVOID)雷区高度, &buffer, 4, &dwNumberOfBytesRead)) {
		return 0;
	}
	lie = buffer;

	//读取雷区数据
	unsigned int 雷区基址2 = 雷区基址;
	for (size_t i = 0; i < lie; i++) {
		for (size_t j = 0; j < hang; j++) {
			byte buffer;
			SIZE_T dwNumberOfBytesRead;
			if (!ReadProcessMemory(h_process, (LPCVOID)雷区基址2, &buffer, 1, &dwNumberOfBytesRead)) {
				return 0;
			}
			雷区基址2++;
			if (buffer != 0x8f) {
				SendMessage(hwnd, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(20 + 16 * j, 70 + 16 * i));
				SendMessage(hwnd, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(20 + 16 * j, 70 + 16 * i));
			}
		}
		雷区基址2 += 32 - hang;
	}

	return 0;
}